﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using blackhouse.JavaScript.JQuery;
using blackhouse.Web.FasterFiles;

namespace blackhouse.Payments.Modules {
    public class NewPaymentFormFields : UserControl {

        #region Properties

        public PaymentTransaction Transaction { get; set; }

        [Browsable(true)]
        [DefaultValue("Przekieruj do strony płatności")]
        public string LinkText { get; set; }

        [Browsable(true)]
        [DefaultValue("payu-userclick")]
        public string CssClass { get; set; }

        [Browsable(true)]
        [DefaultValue(false)]
        public bool Test { get; set; }

        #endregion

        #region Methods

        public NewPaymentFormFields() {
            this.LinkText = "Przekieruj do strony płatności";
            this.CssClass = "payu-userclick";
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            if (this.Visible && (this.Transaction != null && !this.Transaction.Configuration.Testing)) {
                JQueryLibraries jql = new JQueryLibraries();
                jql.IncludeFileBasedOnJQuery("blackhouse.Payments.payment-transaction.js,blackhouse.Payments");

                FasterFilesProvider ffp = new FasterFilesProvider();
                ffp.AddAfterScript(string.Format("SetAutoRedirectToPayU('{0}_lnk');", this.ClientID));
            }
        }

        protected override void Render(HtmlTextWriter writer) {
            if (!this.Visible || this.Transaction == null || this.Transaction.Status != PaymentTransactionStatus.NewTransaction)
                return;

            // pola obowiązkowe
            this.WriteHiddenInput(writer, "pos_id", this.Transaction.Configuration.PosID.ToString());
            this.WriteHiddenInput(writer, "pos_auth_key", this.Transaction.Configuration.PosAuthKey);
            this.WriteHiddenInput(writer, "session_id", this.Transaction.UserSessionId);
            this.WriteHiddenInput(writer, "amount", (this.Transaction.ChargeValue * 100).ToString(CultureInfo.GetCultureInfo("pl-PL")));
            this.WriteHiddenInput(writer, "desc", this.Transaction.Description);
            this.WriteHiddenInput(writer, "first_name", this.Transaction.FirstName);
            this.WriteHiddenInput(writer, "last_name", this.Transaction.LastName);
            this.WriteHiddenInput(writer, "email", this.Transaction.EMail);
            this.WriteHiddenInput(writer, "client_ip", this.Transaction.ClientIP);

            // pola dodatkowe
            if (this.Transaction.Configuration.Testing || this.Test)
                this.WriteHiddenInput(writer, "pay_type", PaymentType.Test.PaymentKey);
            else if (this.Transaction.PaymentType != PaymentType.NoDefined)
                this.WriteHiddenInput(writer, "pay_type", this.Transaction.PaymentType.PaymentKey);

            if (this.Transaction.OrderId > 0)
                this.WriteHiddenInput(writer, "order_id", this.Transaction.OrderId.ToString());
            this.WriteHiddenInput(writer, "desc2", this.Transaction.DescriptionExtension);
            this.WriteHiddenInput(writer, "trsDesc", this.Transaction.BankTransferDescription);
            this.WriteHiddenInput(writer, "street", this.Transaction.Street);
            this.WriteHiddenInput(writer, "street_hn", this.Transaction.StreetNumber);
            this.WriteHiddenInput(writer, "street_an", this.Transaction.FlatNumber);
            this.WriteHiddenInput(writer, "city", this.Transaction.City);
            this.WriteHiddenInput(writer, "country", this.Transaction.Country);
            this.WriteHiddenInput(writer, "phone", this.Transaction.Phone);
            this.WriteHiddenInput(writer, "language", this.Transaction.Language);
            this.WriteHiddenInput(writer, "js", this.Transaction.JsEnable ? "1" : "0");
            this.WriteHiddenInput(writer, "payback_login", this.Transaction.PaybackLogin);
            this.WriteHiddenInput(writer, "sig", this.Transaction.Sig);
            this.WriteHiddenInput(writer, "ts", this.Transaction.Ts);

            writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
            writer.AddAttribute(HtmlTextWriterAttribute.Value, this.LinkText);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_lnk");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
        }

        private void WriteHiddenInput(HtmlTextWriter writer, string name, string value) {
            this.WriteHiddenInput(writer, name, value, false);
        }

        private void WriteHiddenInput(HtmlTextWriter writer, string name, string value, bool forceAdd) {
            if (forceAdd || !string.IsNullOrEmpty(value)) {
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");
                writer.AddAttribute(HtmlTextWriterAttribute.Name, name);
                writer.AddAttribute(HtmlTextWriterAttribute.Value, value.ReplaceEx("\"", "'"));
                writer.RenderBeginTag(HtmlTextWriterTag.Input);
                writer.RenderEndTag();
            }
        }

        #endregion

    }
}